<template>
  <!-- 宠物用户页顶部条 -->
  <div style="height:6vh;padding: 10px 20px;background-color:#fff;">
    <span style="line-height: 60px;font-size:20px;">宠物用户管理</span>
    <el-button type="primary" style="float:right;margin-top: 10px;"
               @click="beforeAddPetUser" >新建宠物用户</el-button>
  </div>
  <!-- 宠物用户弹窗 -->
  <el-dialog
      :title="dialogTitle"
      style="width: 1000px;padding: 40px;"
      v-model="dialogVisible"
      :before-close="handleClose"
  >
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="用户名">
            <el-input placeholder="请输入用户名" v-model="savePetUserForm.name"
                      :disabled="savePetUserForm.id!=null"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号">
            <el-input placeholder="请输入手机号" v-model="savePetUserForm.phone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="邮箱">
            <el-input placeholder="请输入邮箱" v-model="savePetUserForm.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年龄">
            <el-input placeholder="请输入年龄" v-model="savePetUserForm.age"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="用户类型">
            <el-select placeholder="请选择" v-model="savePetUserForm.userType">
              <el-option label="普通用户" value="1"></el-option>
              <el-option label="宠物店主" value="2"></el-option>
              <el-option label="兽医" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="性别">
            <el-radio-group v-model="savePetUserForm.gender">
              <el-radio border label="男" value="1" style="margin: 0;"></el-radio>
              <el-radio border label="女" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户状态">
            <el-radio-group v-model="savePetUserForm.status">
              <el-radio border label="启用" value="1" style="margin: 0;"></el-radio>
              <el-radio border label="禁用" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="savePetUser">保存</el-button>
    </template>
  </el-dialog>

  <!-- 宠物用户搜索卡片 -->
  <el-card style="height: 70px;margin: 20px;">
    <el-form :inline="true">
      <el-form-item label="宠物用户搜索" style="width: 220px;">
        <el-input placeholder="输入用户名"
                  v-model="searchPetUserForm.name" @keydown.enter="loadPetUser"></el-input>
      </el-form-item>
      <el-form-item label="用户状态" style="width: 290px;">
        <el-select placeholder="请选择用户状态"
                   v-model="searchPetUserForm.status" @change="loadPetUser">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadPetUser">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 宠物用户列表表格 -->
  <el-card style="margin: 20px;">
    <el-table :data="petUserArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column label="用户名" prop="name" align="center"></el-table-column>
      <el-table-column label="年龄" prop="age" align="center"></el-table-column>
      <el-table-column label="性别" prop="gender" align="center"></el-table-column>
      <el-table-column label="手机号" prop="phone" align="center"></el-table-column>
      <el-table-column label="邮箱" prop="email" align="center"></el-table-column>
      <el-table-column label="用户状态" prop="status" align="center">
        <template #default="scope">
          <el-switch active-value="1" inactive-value="0" v-model="scope.row.status" @change="changePetUserStatus(scope.row.id,scope.row.status)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="resetPetUserPassword(scope.row.id)">密码重置</el-button>
          <el-button link type="primary" @click="editPetUser(scope.row.id)">编辑</el-button>
          <el-button link type="primary" @click="deletePetUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";



// 定义变量用来控制弹窗是否可见
const dialogVisible = ref(false);
// 定义对象用来收集宠物用户表单数据
const savePetUserForm = ref({
  name: '',
  phone: '',
  email: '',
  age: '',
  userType: '',
  gender: '',
  status: ''
});
// 定义点击"新建宠物用户"按钮时调用的方法
const beforeAddPetUser = () => {
  dialogTitle.value = '新建宠物用户';
  dialogVisible.value = true;
}
// 定义保存宠物用户的方法
const savePetUser = () => {
  let data = qs.stringify(savePetUserForm.value);
  console.log(data);
  axios.post(BASE_URL + '/v1/user/save', data).then((response) => {
    if (response.data.code === 2000) {
      ElMessage.success('保存成功!');
      savePetUserForm.value = {}; // 保存成功后要清空弹窗表单并关闭弹窗
      dialogVisible.value = false;
      // 新增成功后，需要重新加载所有的宠物用户数据
      loadPetUser();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
// 定义处理弹窗关闭的方法
const handleClose = () => {
  if (confirm('您确认要关闭弹窗吗?')) {
    savePetUserForm.value = {};
    dialogVisible.value = false;
  }
}
// 定义变量用来保存宠物用户搜索条件
const searchPetUserForm = ref({name: '', status: ''});
// 定义数组用来保存宠物用户列表表格数据
const petUserArr = ref([]);
// 定义加载宠物用户列表的方法
const loadPetUser = () => {
  console.log(searchPetUserForm.value);
  let data = qs.stringify(searchPetUserForm.value);
  axios.get(BASE_URL + '/v1/user/select?' + data).then((response) => {
    if (response.data.code === 2000) {
      petUserArr.value = response.data.data;
      console.log(petUserArr.value);
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
// 页面加载完毕立即调用加载宠物用户的方法
onMounted(() => {
  loadPetUser();
})
// 定义重置搜索的方法
const resetSearch = () => {
  searchPetUserForm.value = {}; // 清空搜索条件
  loadPetUser(); // 重新加载所有宠物用户数据
}
// 定义变量用来保存弹窗的标题
const dialogTitle = ref('新建宠物用户')
// 定义编辑宠物用户的方法
const editPetUser = (id) => {
  dialogVisible.value = true;
  dialogTitle.value = '编辑宠物用户';
  axios.get(BASE_URL + '/v1/user/select?id=' + id).then((response) => {
    if (response.data.code === 2000) {
      savePetUserForm.value = response.data.data[0];
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
// 定义重置密码的方法
const resetPetUserPassword = (userId) => {
  axios.post(BASE_URL + '/v1/user/update/password/' + userId).then((response) => {
    if (response.data.code === 2000) {
      ElMessage.success('密码重置成功!');
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
// 定义修改宠物用户状态的方法
const changePetUserStatus = (userId, status) => {
  axios.post(BASE_URL + '/v1/user/update/status/' + userId + '/' + status).then((response) => {
    if (response.data.code === 2000) {
      ElMessage.success('状态修改成功!');
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
// 定义删除宠物用户的方法
const deletePetUser = (userId) => {
  if (confirm('您确认要删除当前宠物用户吗?')) {
    axios.post(BASE_URL + '/v1/user/delete/' + userId).then((response) => {
      if (response.data.code === 2000) {
        ElMessage.success('宠物用户删除成功!');
        loadPetUser();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
   }
}
</script>

<style scoped>

</style>